import router from "@/router/index";
export function getSearchFormDataFn(obj: ISusAGetData, dataed: Ref<ISusRGetData>): Ref<ISusRGetData> {
  dataed.value = {
    superFormOpt: obj.superFormOpt,
    searching: false,
    formType: obj.formType,
    formEmitsObj: obj.formEmitsObj,
    formPropsObj: obj.formPropsObj,
    emit: obj.emit
  };
  return dataed;
}
export function getSearchFormMethodsFn(data: Ref<ISusRGetFn>): ISusRGetFn {
  const methods: ISusOMethods = {
    async emitGetListFn() {
      if (!(await data.value.formRef.formValidFn())) return;
      data.value.searching = true;
      data.value.emit("emitGetListFn", {
        fn: () => {
          data.value.searching = false;
          ElMessage.success("查询成功");
        }
      });
    },
    async stoOpenFn() {
      if (!(await data.value.formRef.formValidFn())) return;
      if (!data.value.superFormOpt?.notdialogAdd) return data.value.emit("emitToOpen");
      router.push(`${data.value.superFormOpt?.newRoute}/0`);
    },
    resetFn() {
      data.value.formRef?.resetfileFn(data.value.formPropsObj.formParams);
      methods.emitGetListFn();
    },
    initialFn() {
      let formOpt = data.value.formPropsObj.formOpt;
      type ObjTyped = keyof typeof formOpt.items;
      let item: ObjTyped;
      for (item in formOpt?.items) {
        let itemOpt = formOpt.items[item];
        if (!itemOpt.xl) itemOpt.xl = 6;
        if (!itemOpt.lg) itemOpt.lg = 6;
        if (!itemOpt.md) itemOpt.md = 8;
        if (!itemOpt.sm) itemOpt.sm = 12;
        if (!itemOpt.xs) itemOpt.xs = 24;
        itemOpt.formStyle = JSON.stringify({ marginRight: "30px", ...JSON.parse(itemOpt.formStyle || "{}") });
      }
      data.value.emit("emitChangeProps", { e: formOpt, propsName: ["formOpt"] });
      data.value.formRef?.resetfileFn();
    }
  };
  data.value.formEmitsObj.emitHasChange = ({ e, key, item, form }: IFormItemAChangeRadioNext) => {
    data.value.emit("emitHasChange", { e, key, item, form });
    if (item.refresh) {
      methods.emitGetListFn();
    }
  };
  nextTick(() => {
    methods.initialFn();
  });
  data.value.methods = methods;
  return data.value;
}
